import React, { useEffect, useState } from 'react'
import { data, useParams } from 'react-router-dom'
import { useNavigate } from 'react-router-dom'
import axios from 'axios'
import { NavBar, Card, IndexBar, List, Tag, Button, SearchBar, Space, Toast } from 'antd-mobile'
import { LocationFill } from 'antd-mobile-icons'
import styles from './detail.module.css'
import { dj } from '../../store/City'
import { useDispatch } from 'react-redux'
function Index() {
    const { type } = useParams()
    const dispatch=useDispatch()
    const navigate = useNavigate()
    const [sy, setSy] = useState([])
    const [rem, setRem] = useState([])
    const xuanRan = async () => {
        const res = await axios.get('/api/city')
        console.log(res)
        const { code, data } = res.data
        if (code === 200) {
            setSy(data.city)
            setRem(data.hotCities)
        }

    }
    useEffect(() => {
        xuanRan()
    }, [])
    const [sousuo, setsousuo] = useState([])
    const sous = async (wd) => {
        if (wd === '') {
            Toast.show('请输入内容')
            setsousuo([])
        }
        else {
            const res = await axios.get('/api/city/search', { params: { wd } })
            console.log(res, 'res===')
            const { code, data, message } = res.data
            if (code === 200) {
                setsousuo(data)

            }
            else {
                Toast.show(message)
            }
        }
    }
    const xz = (city) => {
        dispatch(dj({ city, type }))
        navigate('/')
    }
    return (
        <div>
            <NavBar onBack={() => navigate('/')}>
                <SearchBar placeholder='请输入内容'
                    onChange={(wd) => sous(wd)}
                />
            </NavBar>
            {
                sousuo.length > 0 ? <div className={styles.hh}>
                    <List >
                        {
                            sousuo.map((item,index)=>{
                                return <List.Item key={index}>{ item.name}</List.Item>
                            })
                        }
                    </List>
                </div> : ''
            }
            <Card title={<div>当前城市：<LocationFill /> <span>{rem.length > 0 && rem[0].name}</span></div>} >

            </Card>
            <Card title='热门城市' >
                {
                    rem.length > 0 && rem.map((item, index) => {
                        return <Tag key={index}>{item.name}</Tag>
                    })

                }
            </Card>
            <div style={{ height: window.innerHeight }}>
                <IndexBar>
                    {sy.length > 0 && sy.map(item => {
                        return (
                            <IndexBar.Panel
                                index={item.initial}
                                title={`${item.initial}`}
                                key={`${item.initial}`}
                            >
                                <List>
                                    {item.list.map((item, index) => (
                                        <List.Item key={index} onClick={()=>xz(item.name)}>{item.name}</List.Item>
                                    ))}
                                </List>
                            </IndexBar.Panel>
                        )
                    })}
                </IndexBar>
            </div>
        </div>
    )
}

export default Index
